package interview.i_001_algorithm;

import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 最近最少使用，一种常用的页面置换算法
 * https://leetcode-cn.com/problems/lru-cache
 */
public class T001_LRU {
    static class LRUCache<K, V> extends LinkedHashMap<K, V> {

        int capacity;

        public LRUCache(int initialCapacity) {
            super(initialCapacity, 0.75f, true);
            this.capacity = initialCapacity;
        }

        @Override
        protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
            return super.size() > capacity;
        }
    }

    public static void main(String[] args) {
        LRUCache<Integer, Integer> lRUCache = new LRUCache<>(2);
        lRUCache.put(1, 1); // 缓存是 {1=1}
        lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
        System.out.println(lRUCache);
        lRUCache.get(1);    // 返回 1
        lRUCache.put(3, 3); // 该操作会使得关键字 2 作废，缓存是 {1=1, 3=3}
        System.out.println(lRUCache);
        lRUCache.get(2);    // 返回 -1 (未找到)
        lRUCache.put(4, 4); // 该操作会使得关键字 1 作废，缓存是 {4=4, 3=3}
        System.out.println(lRUCache);
        lRUCache.get(1);    // 返回 -1 (未找到)
        lRUCache.get(3);    // 返回 3
        lRUCache.get(4);    // 返回 4

        System.out.println("===================================================================");

        LRUCacheDemo<Integer, Integer> lruCacheDemo = new LRUCacheDemo<>(2);
        lruCacheDemo.put(1, 1); // 缓存是 {1=1}
        lruCacheDemo.put(2, 2); // 缓存是 {1=1, 2=2}
        System.out.println(lruCacheDemo);
        lruCacheDemo.get(1);    // 返回 1
        lruCacheDemo.put(3, 3); // 该操作会使得关键字 2 作废，缓存是 {1=1, 3=3}
        System.out.println(lruCacheDemo);
        lruCacheDemo.get(2);    // 返回 -1 (未找到)
        lruCacheDemo.put(4, 4); // 该操作会使得关键字 1 作废，缓存是 {4=4, 3=3}
        System.out.println(lruCacheDemo);
        lruCacheDemo.get(1);    // 返回 -1 (未找到)
        lruCacheDemo.get(3);    // 返回 3
        System.out.println(lruCacheDemo.get(4));    // 返回 4
    }

    @ToString(exclude = {"key", "perv", "next"})
    @NoArgsConstructor
    static class Node<K, V> {
        K key;
        V value;
        Node<K, V> perv;
        Node<K, V> next;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }
    }

    static class DoubleLinkList<K, V> {
        Node<K, V> head;
        Node<K, V> tail;

        public DoubleLinkList() {
            head = new Node<>();
            tail = new Node<>();
            head.next = tail;
            tail.perv = head;
        }

        public void addHead(Node<K, V> node) {
            node.perv = head;
            node.next = head.next;
            head.next.perv = node;
            head.next = node;
        }

        public void removeNode(Node<K, V> node) {
            node.perv.next = node.next;
            node.next.perv = node.perv;
            node.perv = null;
            node.next = null;
        }
    }

    static class LRUCacheDemo<K, V> {
        int cacheSize;
        Map<K, Node<K, V>> map;
        DoubleLinkList<K, V> doubleLinkList;

        public LRUCacheDemo(int initialCapacity) {
            cacheSize = initialCapacity;
            map = new HashMap<>();
            doubleLinkList = new DoubleLinkList<>();
        }

        public void put(K key, V value) {
            if (map.containsKey(key)) {
                Node<K, V> node = map.get(key);
                node.value = value;
                doubleLinkList.removeNode(node);
                doubleLinkList.addHead(node);
            } else {
                if (map.size() == cacheSize) {
                    Node<K, V> node = doubleLinkList.tail.perv;
                    map.remove(node.key);
                    doubleLinkList.removeNode(node);
                }
                Node<K, V> node = new Node<>(key, value);
                map.put(key, node);
                doubleLinkList.addHead(node);
            }
        }

        public V get(K key) {
            if (map.containsKey(key)) {
                Node<K, V> node = map.get(key);
                doubleLinkList.removeNode(node);
                doubleLinkList.addHead(node);
                return node.value;
            }
            return null;
        }

        @Override
        public String toString() {
            return map.toString();
        }
    }
}
